﻿using System.Text;
using System.Runtime.Serialization;

namespace Order
{
    [DataContract]
    public class OrderDetail
    {
        [DataMember]
        public Commodity[] Commodities { get; set; }
        public override bool Equals(object obj)
        {
            if (obj == this) return true;
            if (obj == null || !(obj is OrderDetail d)) return false;
            int size = Commodities.Length;
            var dCom = d.Commodities;
            if (size != dCom.Length) return false;
            for(int i = 0; i < size; i++)
            {
                if (!Commodities[i].Equals(dCom[i])) return false;
            }
            return true;
        }

        public override int GetHashCode()
        {
            int hash = 0;
            foreach(var c in Commodities)
            {
                hash ^= c.GetHashCode();
            }
            return hash;
        }

        public override string ToString()
        {
            if (Commodities == null || Commodities.Length == 0) return "";
            StringBuilder builder = new StringBuilder();
            foreach (var item in Commodities)
            {
                builder.Append($"Commodity:\t{item}\n");
            }
            return builder.ToString(0, builder.Length - 1);
        }
    }
}
